<?php
/** .-------------------------------------------------------------------
* |  Software: [HDPHP framework]
* |      Site: www.hdphp.com
* |-------------------------------------------------------------------
* |    Author: 向军 <2300071698@qq.com>
* |    WeChat: aihoudun
* | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
* '-------------------------------------------------------------------*/

namespace system\model;

use app\admin\controller\Orders;
use hdphp\Model\Model;

class Order extends Model{
	//数据表
	protected $table = "orders";

	//允许填充字段
	protected $allowFill = [ ];

	//禁止填充字段
	protected $denyFill = [ ];

	//完整表名
	protected $full = false;

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
		['time','time','function',self::MUST_AUTO,self::MODEL_INSERT],

	];

	//自动过滤
    protected $filter=[
        //[表单字段名,过滤条件,处理时间]

    ];

	//时间操作,需要表中存在created_at,updated_at字段
	protected $timestamps=false;

	public function order(){

		$adid=Q('post.adid');
//		p($_SESSION);die;
		$uid=$_SESSION['home']['uid'];
		$order=new Order();
		$addData=Db::table('address')->where('adid',$adid)->first();
		$order['consignee']=$addData['adname'];
		$order['address']=$addData['address'];
		$order['mobile']=$addData['phone'];
		$order['total']=$_SESSION['cart']['total'];
		$order['number']=Cart::getOrderId();
		$order['remark']=Q('post.remark');
		$order['uid']=$uid;
		$order['status']='未付款';
		$oid=$order->save();



		$orderList=new \system\model\Order_list();
		$orderList['oid']=$oid;
		$orderList['quantity']=$_SESSION['cart']['total_rows'];
		$orderList['subtotal']=$_SESSION['cart']['total'];
		$orderList['explain']=Q('post.remark');
		$gg=$_SESSION['cart']['goods'];

		foreach($gg as $k=>$v){
			$orderList['spec']=$v['options'];
			$orderList['gid']=$v['id'];
		}
//		p($gg);die;
		$orderList['glnumber']=Cart::getOrderId();;
		$orderList->save();
		unset($_SESSION['cart']);
		return $oid;

	}

	//编辑
	public function edit(){

		$oid=Q('post.oid');
		$orderData=Order::find($oid);
		$orderData['status']=Q('post.status');
		return $orderData->save();

	}

}